package model.action;

import java.util.List;
import datacontainer.ImmuTable;


/**
 * 
 * @author Michael Deng
 * 
 */
public abstract class TableAction extends AbstractAction {

    @Override
    public void setInput (String column, List<String> conditions) {

    }

    /**
     * Method responsible for executing the SumColumnTable action.
     */
    protected void sumColumnTableAction (ImmuTable workSpace) {
        List<List<String>> data = workSpace.getData();
        List<String> type = workSpace.getTypes();
        for (int i = 0; i < data.size(); i++) {
            sumColumn(data.get(i), type.get(i));
        }
    }

    /**
     * Accepts the column and the index of the column in the workspace.
     * 
     * @param column
     * @param columnIndex
     * @return The original column plus an additional entry. This final entry is
     *         the sum of the column.
     */
    private void sumColumn (List<String> column, String columnType) {
        String columnSum = "";
        if ("Double".equals(columnType)) {
            columnSum = Double.toString(determineSum(column));
        }
        column.add(columnSum);
    }

    /**
     * Adds up each element in the double array.
     * 
     * @param column
     * @return
     */
    private double determineSum (List<String> column) {
        double sum = 0;

        for (String i : column) {
            if (!"".equals(i)) {
                sum = sum + Double.parseDouble(i);
            }
        }
        return sum;
    }
}
